package com.simen.hellobytes.encrypt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

/**
 * @author Created by Simen.
 * @date 创建日期 2018/9/19 17:39
 * @modify 修改者 Simen
 */
public class RSAHelpUtil {

    private static final String BEGIN_PUB_RSA_KEY = "BEGIN RSA PUBLIC KEY";
    private static final String END_PUB_RSA_KEY = "END RSA PUBLIC KEY";

    private static final String BEGIN_PRI_RSA_KEY = "BEGIN RSA PRIVATE KEY";
    private static final String END_PRI_RSA_KEY = "END RSA PRIVATE KEY";

    private static final String BEGIN_PUB_KEY = "BEGIN PUBLIC KEY";
    private static final String END_PUB_KEY = "END PUBLIC KEY";

    private static final String BEGIN_PRI_KEY = "BEGIN PRIVATE KEY";
    private static final String END_PRI_KEY = "END PRIVATE KEY";

    private static final String BEGIN_ENCPT_PRI_KEY = "BEGIN ENCRYPTED PRIVATE KEY";
    private static final String END_ENCPT_PRI_KEY = "END ENCRYPTED PRIVATE KEY";

    public static String clearPubPem(String keyStr) throws IOException {
        BufferedReader bufferedReader = null;
        String line = null;
        try {
            bufferedReader = new BufferedReader(new StringReader(keyStr));

            StringBuilder resultBuilder = new StringBuilder();
            int lineNumber = 0;

            while ((line = bufferedReader.readLine()) != null) {
                if (lineNumber == 0) {
                    line = line.replaceAll(BEGIN_PUB_RSA_KEY, "")
                            .replaceAll(END_PUB_RSA_KEY, "")
                            .replaceAll(BEGIN_PUB_KEY, "")
                            .replaceAll(END_PUB_KEY, "")
                            .replaceAll("-", "");
                } else {
                    line = line.replaceAll(END_PUB_RSA_KEY, "")
                            .replaceAll(END_PUB_KEY, "")
                            .replaceAll("-", "");
                }

                resultBuilder.append(line);

                lineNumber++;
            }

            return resultBuilder.toString();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                }
            }
        }

    }

    public static String clearPriPem(String keyStr) throws IOException {
        BufferedReader bufferedReader = null;
        String line = null;
        try {
            bufferedReader = new BufferedReader(new StringReader(keyStr));

            StringBuilder resultBuilder = new StringBuilder();
            int lineNumber = 0;

            while ((line = bufferedReader.readLine()) != null) {
                if (lineNumber == 0) {
                    line = line.replaceAll(BEGIN_PRI_RSA_KEY, "")
                            .replaceAll(END_PRI_RSA_KEY, "")
                            .replaceAll(BEGIN_PRI_KEY, "")
                            .replaceAll(END_PRI_KEY, "")
                            .replaceAll(BEGIN_ENCPT_PRI_KEY, "")
                            .replaceAll(END_ENCPT_PRI_KEY, "")
                            .replaceAll("-", "");
                } else {
                    line = line.replaceAll(END_PRI_RSA_KEY, "")
                            .replaceAll(END_PRI_KEY, "")
                            .replaceAll(END_ENCPT_PRI_KEY, "")
                            .replaceAll("-", "");
                }

                resultBuilder.append(line);

                lineNumber++;
            }

            return resultBuilder.toString();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                }
            }
        }

    }

}
